package com.lry.jvm.jnative.java.lang;

import com.lry.jvm.jnative.NativeMethod;
import com.lry.jvm.jnative.Registry;
import com.lry.jvm.rtda.Frame;
import com.lry.jvm.rtda.JObject;
import com.lry.jvm.rtda.heap.JClass;
import com.lry.jvm.rtda.heap.JClassLoader;
import com.lry.jvm.rtda.heap.StringPool;

public class _Class {

    public static void init(Frame frame) {
        Registry.register("java/lang/Class", "getPrimitiveClass", "(Ljava/lang/String;)Ljava/lang/Class;", getPrimitiveClass(frame));
        Registry.register("java/lang/Class", "getName0", "()Ljava/lang/String;", getName0(frame));
        Registry.register("java/lang/Class", "desiredAssertionStatus0", "(Ljava/lang/Class;)Z", desiredAssertionStatus0(frame));
    }

    public static NativeMethod getPrimitiveClass(Frame frame){
        JObject obj = frame.getLocalVars().getRef(0);
        String name = StringPool.javaString(obj);
        JClassLoader loader = frame.getMethod().getJClass().getLoader();
        JObject classObj = loader.loadClass(name).getJClass();
        frame.getOperandStack().pushRef(classObj);
        return new NativeMethod(frame);
    }

    public static NativeMethod getName0(Frame frame){
        JObject obj = frame.getLocalVars().getRef(0);
        JClass cla = (JClass) obj.getExtra();
        String name = cla.javaName();
        JObject nameObj = StringPool.JString(cla.getLoader(),name);
        frame.getOperandStack().pushRef(nameObj);
        return new NativeMethod(frame);
    }

    public static NativeMethod desiredAssertionStatus0(Frame frame){
        frame.getOperandStack().pushBoolean(false);
        return new NativeMethod(frame);
    }
}
